package com.ycy.temperature.view.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

import com.jude.utils.JUtils;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.ycy.temperature.App;
import com.ycy.temperature.R;
import com.ycy.temperature.bean.LabelBean;
import com.ycy.temperature.bean.VerificationBean;
import com.ycy.temperature.date.net.UserNetWorks;
import com.ycy.temperature.date.utils.CheckUtils;
import com.ycy.temperature.view.base.BaseActivity;
import com.ycy.temperature.view.view.FlowLayout;
import com.ycy.temperature.view.view.TitleView;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.qqtheme.framework.picker.DateTimePicker;
import rx.Subscriber;

/**
 * Created by MI on 2018/7/24.
 */

public class ActivityActivity extends BaseActivity {
    @BindView(R.id.title)
    TitleView title;
    @BindView(R.id.Text_Title)
    AutoCompleteTextView TextTitle;
    @BindView(R.id.Text_Content)
    AutoCompleteTextView TextContent;
    @BindView(R.id.Text_Time)
    TextView TextTime;
    @BindView(R.id.Layout_Time)
    AutoLinearLayout LayoutTime;
    @BindView(R.id.Layout_Label)
    FlowLayout LayoutLabel;

    String[] StrLabel = {};
    List<String> ListLabel;


    @Override
    protected int getLayout() {
        return R.layout.activity_activity;
    }

    @Override
    protected void setData() {

    }

    @Override
    protected void init(Bundle inBundle) {
        ButterKnife.bind(this);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
        title.getLeft_ibtn().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title.getRight_tv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!getValidate()) return;
                getActivity();
            }
        });
        ListLabel = new ArrayList<>();
        ListLabel.addAll(Arrays.asList(StrLabel));
        getLabel();
    }

    public boolean getValidate() {
        if (CheckUtils.isEmpty(TextTitle.getText().toString())) {
            JUtils.Toast("请输入标题");
            return false;
        }
        if (CheckUtils.isEmpty(TextContent.getText().toString())) {
            JUtils.Toast("请输入内容");
            return false;
        }
        if (CheckUtils.isEmpty(TextTime.getText().toString())) {
            JUtils.Toast("请选择时间");
            return false;
        }
        if (ListLabel.size() == 0) {
            JUtils.Toast("请选择标签");
            return false;
        }
        return true;
    }

    public void getActivity() {
        UserNetWorks.getActivity("0", TextTitle.getText().toString(), TextContent.getText().toString(), TextTime.getText().toString(), ListLabel + "", App.getInstance().StrCity, new Subscriber<VerificationBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                JUtils.Toast("网络错误");
            }

            @Override
            public void onNext(VerificationBean verificationBean) {
                if (verificationBean.getResponseState().equals("1")) {
                    JUtils.Toast(verificationBean.getMsg());
                    finish();
                } else {
                    JUtils.Toast(verificationBean.getMsg());
                }
            }
        });
    }

    public void getLabel() {
        UserNetWorks.getLabel("", new Subscriber<LabelBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                JUtils.Toast("网络错误");
            }

            @Override
            public void onNext(LabelBean labelBean) {
                if (labelBean.getResponseState().equals("1")) {
                    List<LabelBean.DataBean> dataBeen = labelBean.getData();
                    // 设置文字大小
                    LayoutLabel.setTextSize(15);
                    // 设置文字颜色
                    LayoutLabel.setTextColor(R.color.bg_Gray);
                    // 设置文字背景
                    LayoutLabel.setBackgroundResource(R.drawable.bg_frame);

                    // 设置文字水平margin
                    LayoutLabel.setHorizontalSpacing(15);
                    // 设置文字垂直margin
                    LayoutLabel.setVerticalSpacing(15);

                    // 设置文字水平padding
                    LayoutLabel.setTextPaddingH(20);
                    // 设置文字垂直padding
                    LayoutLabel.setTextPaddingV(8);

                    // 设置UI与点击事件监听
                    // 最后调用setFlowLayout方法
                    LayoutLabel.setFlowLayout(dataBeen, new FlowLayout.OnItemClickListener() {
                        @Override
                        public void onItemClick(String content, String id, boolean judge) {
                            if (!judge) {
                                for (int i = 0; i < ListLabel.size(); i++) {
                                    if (ListLabel.get(i).equals(content))
                                        ListLabel.remove(i);
                                }
                            } else {
                                ListLabel.add(content);
                            }
                        }
                    });
                }
            }
        });
    }


    public void onYearMonthDayTimePicker(View view) {
        DateTimePicker picker = new DateTimePicker(this, DateTimePicker.HOUR_24);
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        picker.setDateRangeStart(year, month, day);
        picker.setDateRangeEnd(2038, 12, 31);
        picker.setTimeRangeStart(hour, minute);
        picker.setTimeRangeEnd(23, 59);
        picker.setTopLineColor(0x9981B0F2);
        picker.setLabelTextColor(0xFF81B0F2);
        picker.setDividerColor(0xFF81B0F2);
        picker.setOnDateTimePickListener(new DateTimePicker.OnYearMonthDayTimePickListener() {
            @Override
            public void onDateTimePicked(String year, String month, String day, String hour, String minute) {
                TextTime.setText(year + "-" + month + "-" + day + " " + hour + ":" + minute);
            }
        });
        picker.show();
    }

    @OnClick({R.id.Layout_Time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.Layout_Time:
                onYearMonthDayTimePicker(view);
                break;
        }
    }
}
